package com.firstsight.micro.homedo.test.stream;

import lombok.Data;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.PDFRenderer;
import org.springframework.util.CollectionUtils;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * Created on 2021/5/21.
 *
 * @author yanshuchun
 */
public class InputAndOutputStream {
    public static String parm = "https://project-homedo.oss-cn-shanghai.aliyuncs.com/d079cd1d-e264-483c-943b-14e8e86bbe5e_aaaaaaaaaaaaaaaaaaaaaaa.pdf";

    public static void main(String[] args) {
        buffer(parm,"png");
    }
    public static void buffer(String formUrl,String imgType) {
        try {
            URL url = new URL(formUrl);
            PDDocument pdDocument = PDDocument.load(url.openStream());
            PDFRenderer renderer = new PDFRenderer(pdDocument);
            int numberOfPages = pdDocument.getNumberOfPages();
            ArrayList<BufferedImage> bufferedImageList = new ArrayList<>();
            for (int i = 0; i < numberOfPages; i++) {
                bufferedImageList.add(renderer.renderImageWithDPI(i, 250));
            }
            deal(bufferedImageList,imgType);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Data
    static class ImageTemp {
        Integer height;
        int[] imgRGBArray;
    }

    public static void deal(ArrayList<BufferedImage> bufferedImageList,String imgType) {
        if (!CollectionUtils.isEmpty(bufferedImageList)) {
            List<ImageTemp> imageTemps = new ArrayList<>(bufferedImageList.size());
            AtomicInteger heigh = new AtomicInteger();
            BufferedImage bufferedImage = bufferedImageList.get(0);
            int width = bufferedImage.getWidth();

//            获取图片信息
            bufferedImageList.stream().forEach(imageBuffer -> {
                ImageTemp imageTemp = new ImageTemp();
                int[] imgRGBArray = new int[imageBuffer.getWidth() * imageBuffer.getHeight()];
                imageBuffer.getRGB(0, 0,
                        imageBuffer.getWidth(), imageBuffer.getHeight(), imgRGBArray, 0, imageBuffer.getWidth());
                imageTemp.setHeight(imageBuffer.getHeight());
                imageTemp.setImgRGBArray(imgRGBArray);
                heigh.addAndGet(imageBuffer.getHeight());
                imageTemps.add(imageTemp);
            });
            BufferedImage imageResult = new BufferedImage(width, heigh.get(), BufferedImage.TYPE_INT_RGB);
            AtomicInteger y = new AtomicInteger();
            imageTemps.stream().forEach(imageTemp -> {
                int height = imageTemp.getHeight();
                imageResult.setRGB(0, y.get(), width, height, imageTemp.getImgRGBArray(), 0, width);
                y.addAndGet(height);
            });
            try {
                ByteArrayOutputStream os = new ByteArrayOutputStream();
                ImageIO.write(imageResult, imgType, os);
                InputStream input = new ByteArrayInputStream(os.toByteArray());

            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

}
